var ACTION_KEYDOWN_LEFT = 0;
var ACTION_KEYDOWN_RIGHT = 1;
var ACTION_KEYDOWN_UP = 2;
var ACTION_KEYDOWN_DOWN = 3;
var ACTION_KEYDOWN_DOUBLEKEY = 4;
var ACTION_KEYUP_LEFT = 5;
var ACTION_KEYUP_RIGHT = 6;
var ACTION_KEYUP_UP = 7;
var ACTION_KEYUP_DOWN = 8;
var ACTION_KEYUP_ENTER = 9;
var ACTION_KEYUP_DOUBLEKEY = 10;


var listeners = [];

var keyTimerId = 0;
var timerLength = 500;
var numberOfTimers = 0;
var keyPressedOne = -1;
var keyPressedTwo = -1;
var actionsEnabled = true;

document.onkeyup = onKeyUp;
document.onkeydown = onKeyDown;

function onKeyDown(e) {
	//if actions are not enabled return out of funtion
	if(!actionsEnabled){
		return;	
	}
	
	var keyId = (window.event) ? event.keyCode : e.keyCode;
	if(keyPressedTwo == -1 && keyId != keyPressedOne){
		
		//if 2 different keys have been pressed fire double press event
		//and cancel the first key press event
		if(keyPressedOne != -1){
			keyPressedTwo = keyId;
			clearTimeout(keyTimerId);
			numberOfTimers = 0;			
			twoKeysDown();
			
		}else{
			keyPressedOne = keyId;
//			$('a').innerHTML = keyPressedOne;
			switch(keyId){
			case 37:
			keyLeft();
			break;
			case 38:
			keyUp();
			break;
			case 39:
			keyRight();
			break;
			case 40:
			keyDown();
			break;
		}
		}
	}
}

function onKeyUp(e) {
	//if actions are not enabled return out of funtion
	if(!actionsEnabled){
		return;	
	}
	
	var keyId = (window.event) ? event.keyCode : e.keyCode;
	clearTimeout(keyTimerId);
	numberOfTimers = 0;

	//if both keys were pressed down, dont call up method until both keys are unpressed
	if(keyPressedTwo != -1){
		if(keyPressedOne != -1){
			keyPressedOne = -1;
		}else{
			keyPressedTwo = -1;
			twoKeysUp();
		}
	}else{	
		keyPressedOne = -1;
		switch(keyId){
				case 37:
				keyUpLeft();
				break;
				case 38:
				keyUpUp();
				break;
				case 39:
				keyUpRight();
				break;
				case 40:
				keyUpDown();
				break;
				case 67:
				keyEnter();
				break;
		}
	}
}

function addListener(callback){
	listeners.push(callback);
}

function keyLeft(){
	var tmpLoc = CURRENT_LOCATION;
	numberOfTimers++;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYDOWN_LEFT, tmpLoc);
	}
	
	 keyTimerId = setTimeout( "keyLeft()", timerLength);
}

function keyUp(){
	var tmpLoc = CURRENT_LOCATION;
	numberOfTimers++;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYDOWN_UP, tmpLoc);
	}
	
	 keyTimerId = setTimeout( "keyUp()", timerLength);
		 
}

function keyRight(){
	var tmpLoc = CURRENT_LOCATION;
	numberOfTimers++;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYDOWN_RIGHT, tmpLoc);
	}
	
	 keyTimerId = setTimeout( "keyRight()", timerLength);
		 
}

function keyDown(){
	var tmpLoc = CURRENT_LOCATION;
	numberOfTimers++;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYDOWN_DOWN, tmpLoc);
	}
	
	keyTimerId = setTimeout( "keyDown()", timerLength);
		 
}

function keyUpLeft(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYUP_LEFT, tmpLoc);
	}
}

function keyUpUp(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYUP_UP, tmpLoc);
	}
}

function keyUpRight(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYUP_RIGHT, tmpLoc);
	}
}

function keyUpDown(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYUP_DOWN, tmpLoc);
	}
}

function keyEnter(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYUP_ENTER, tmpLoc);
	}
}

function twoKeysDown(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYDOWN_DOUBLEKEY, tmpLoc);
	}
}

function twoKeysUp(){
	var tmpLoc = CURRENT_LOCATION;
	
	for (var i=0; i<listeners.length; i++) {
		listenerMethod = listeners[i];
		listenerMethod(ACTION_KEYUP_DOUBLEKEY, tmpLoc);
	}
}

function getIndexIncrement(){
	if(numberOfTimers < 3){
		return 1;
	}else if(numberOfTimers == 3){
		return 2;
	}else if(numberOfTimers == 4){
		return 3;
	}else if(numberOfTimers == 5){
		return 5;
	}else if(numberOfTimers > 5){
		return 10;
	}
}